package com.genshang.restaurant.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genshang.restaurant.entity.Record1;
import com.genshang.restaurant.mapper.Record1Mapper;
import com.genshang.restaurant.result.ResultBody;
import com.genshang.restaurant.service.IRecord1Service;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Alex
 * @since 2020-09-25
 */
@Service
public class Record1ServiceImpl extends ServiceImpl<Record1Mapper, Record1> implements IRecord1Service {

    @Override
    public ResultBody getRate() {
        QueryWrapper<Record1> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", "ababaaaabXN: com.restaurant.application.score.Score");
        queryWrapper.or().eq("id", "ababaaaabXO: com.restaurant.application.score.Score");
        List<Record1> record1List = this.list(queryWrapper);
        String defaultShopOne = "{\"id\":\"108037494980734976\",\"shop\":\"ababaaaabH5: com.restaurant.application.shop.Shop\",\"shopId\":\"02\",\"shopName\":\"艮上·铜仁路店\",\"paying\":20,\"score\":1,\"createTime\":1573045680044,\"modifyTime\":1601019513265}";
        String defaultShopTwo = "{\"id\":\"108037532142268416\",\"shop\":\"ababaaaabH1: com.restaurant.application.shop.Shop\",\"shopId\":\"01\",\"shopName\":\"艮上·环宇荟店\",\"paying\":10,\"score\":1,\"createTime\":1573045688904,\"modifyTime\":1573045688904}";
        Map shopOneMap = JSON.parseObject(defaultShopOne);
        Map shopTwoMap = JSON.parseObject(defaultShopTwo);
        List<Map> defaultResultList = new ArrayList<>();
        defaultResultList.add(shopOneMap);
        defaultResultList.add(shopTwoMap);
        if (CollectionUtils.isEmpty(record1List)) {
            return ResultBody.success(defaultResultList);
        }
        List<Map> resultList = new ArrayList<>();
        for (Record1 record1 : record1List) {
            String obj = record1.getObject();
            Map map = JSON.parseObject(obj);
            if (map != null && map.get("data") != null) {
                Map shopMap = JSON.parseObject(map.get("data").toString());
                resultList.add(shopMap);
            } else {
                return ResultBody.success(defaultResultList);
            }
        }
        return ResultBody.success(resultList);
    }
}
